package com.neusoft.controller;

import com.neusoft.po.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.*;

import com.neusoft.po.DailyTotal;
import com.neusoft.po.Orders;
import com.neusoft.service.OrdersService;

import java.math.BigDecimal;
import java.util.List;

@RefreshScope
@RestController
@RequestMapping("/OrdersController")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @PostMapping("/createOrders/{userId}/{businessId}/{daId}/{orderTotal}")
    public CommonResult<Integer> createOrders(@PathVariable("userId") String userId,
                                     @PathVariable("businessId") Integer businessId,
                                     @PathVariable("daId") Integer daId,
                                     @PathVariable("orderTotal") BigDecimal orderTotal) throws Exception {
        Orders orders = new Orders();
        orders.setUserId(userId);
        orders.setBusinessId(businessId);
        orders.setDaId(daId);
        orders.setOrderTotal(orderTotal);
        int orderId = ordersService.createOrders(orders);
        return new CommonResult<>(200,"sucess",orderId);
    }

    @GetMapping("/getOrdersById/{orderId}")
    public CommonResult<Orders> getOrdersById(
            @PathVariable("orderId") Integer orderId) throws Exception{
        Orders orders = ordersService.getOrdersById(orderId);
        return new CommonResult(200,"success",orders);
    }

    @GetMapping("/listOrdersByUserId/{userId}")
    public CommonResult<List> listOrdersByUserId(
            @PathVariable("userId") String userId) throws Exception {
        List<Orders>list = ordersService.listOrdersByUserId(userId);
        return new CommonResult(200,"success",list);
    }

    @PutMapping("/turnOrderState/{orderId}")
    public CommonResult<Integer> turnOrderState(
            @PathVariable("orderId") Integer orderId) throws Exception {
        Integer isOk = ordersService.turnOrderState(orderId);
        return new CommonResult(200,"success",isOk);
    }

    @GetMapping("/listPaidOrdersByUserId/{userId}")
    public CommonResult<List> listPaidOrdersByUserId(
            @PathVariable("userId") String userId) throws Exception {
        List<Orders>list = ordersService.listPaidOrdersByUserId(userId);
        return new CommonResult(200,"success",list);
    }

    @PutMapping("/updateAddMark/{orderId}/{addMark}")
    public CommonResult<Integer> updateAddMark(
            @PathVariable("orderId") Integer orderId,
            @PathVariable("addMark") Integer addMark) throws Exception {
        Integer isOk = ordersService.updateAddMark(orderId, addMark);
        return new CommonResult(200,"success",isOk);
    }

    @GetMapping("/listWeeklyTotalById/{userId}")
    public CommonResult<List> listWeeklyTotalById(
            @PathVariable("userId") String userId) throws Exception {
        List<DailyTotal> list = ordersService.listWeeklyTotalById(userId);
        return new CommonResult(200,"success",list);
    }

    @GetMapping("/listMonthlyTotalById/{userId}")
    public CommonResult<List> listMonthlyTotalById(
            @PathVariable("userId") String userId) throws Exception {
        List<DailyTotal> list = ordersService.listMonthlyTotalById(userId);
        return new CommonResult(200,"success",list);
    }
}
